package com.platform.service;

import com.platform.dao.ApiAgentMapper;
import com.platform.entity.AgentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 商品对应规格表值表Service实现类
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2018-10-07 20:51:28
 */
@Service()
public class ApiAgentService {
    @Autowired
    private ApiAgentMapper agentMapper;

    public AgentVo applyAgent(Long vendorRootDeptId, Long agentRootDeptId, Long applyUserId, Integer applyAgentUserGrade){

        //先检查代理关系是否存在
        int exist = checkAgentApply(vendorRootDeptId, agentRootDeptId);
        if(exist > 0){
            return null;
        }

        //建立代理信息
        AgentVo vo = new AgentVo();
        vo.setVendorRootDeptId(vendorRootDeptId);
        vo.setAgentRootDeptId(agentRootDeptId);
        vo.setApplyUserId(applyUserId);
        vo.setApplyTime(new Date());
        vo.setIsCert(0);
        vo.setCertLevel(applyAgentUserGrade);
        agentMapper.save(vo);
        return vo;
    }

    public int checkAgentApply(Long vendorRootDeptId, Long agentRootDeptId){
        //先检查代理关系是否存在
        return agentMapper.checkAgentApply(vendorRootDeptId, agentRootDeptId);
    }

    public List<AgentVo> getAgentApplyByVendorAndAgent(Long vendorRootDeptId, Long agentRootDeptId){
        return agentMapper.getAgentApplyByVendorAndAgent(vendorRootDeptId, agentRootDeptId);
    }

    public int checkVendorUser(Long vendorRootDeptId, Long applyUserId){
        //先检查指定用户是否为供应商的代理商
        return agentMapper.checkVendorUser(vendorRootDeptId, applyUserId);
    }
}
